/*
 *  $Id: Scorch.cs 1189 2010-11-12 14:40:06Z thenn.erannor $
 *  This file is a part of Ragima CCG.
 *  (C) 2008 Ragima development group.
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 */

using Ragima.Helper;

namespace Ragima.Affects {
    /// <summary>
    /// Эффект: ожог
    /// Параметры: значение (сила ожога)
    /// При каждом активном действии наносит игроку или существу урон, равный силе ожога.
    /// </summary>
    public class Scorch : FlagAffect {
        public readonly int Value;

        public Scorch(int value) {
            Value = value;
        }

        public override string Id {
            get { return "scorch"; }
        }
        public override string NameText {
            get { return Name.GetText(Value); }
        }
        public override ElementType Element {
            get { return ElementType.Fire; }
        }

        public override string DescriptionText {
            get { return Description.GetText(Value); }
        }

        public override int BaseWeight {
            get { return Value * -25; }
        }

        public override AffectIcon Icon {
            get { return AffectIcon.Scorch; }
        }

        public override bool Drawable {
            get { return Owner.HasAffect(this); }
        }

        public override Affect Clone() {
            return new Scorch(Value);
        }

        public override void SetAffect() {
            base.SetAffect();
            LocalSummary.OnMove += DoScorchOnMove;
            LocalSummary.OnUseAbility += DoScorchOnAbility;
            LocalSummary.OnOffenceReady += DoScorchOnCreature;
            LocalSummary.OnDefenceReady += DoScorchOnCreature;
            Region region = Owner.FindRegion();
            if (region != null) {
                UI.ParticleInfo info = new UI.ParticleInfo(Data.Particles.Fire01);
                info.AddPhase(0, 0, 0xfa733c.ToColor(), 0.1, 0.7, 0, UI.ParticleTrajectory.Line(-90, 0.2));
                info.AddPhase(20, 40, 0xfa733c.ToColor(), 0.5, 1);
                info.AddPhase(40, 0, 0x101010.ToColor(), 0.1, 0.6);
                UI.ParticleEmitterInfo emitter = new UI.ParticleEmitterInfo();
                emitter.Duration = 100;
                emitter.Rate = UI.ParticleRate.Always(30);
                emitter.Generator = UI.ParticleGenerator.RandomAngle(info, UI.ParticlePoints.EngulfFromBottom(region.Rect, 1), 60, 90);
                Application.CurrentScene.AddEmitter(emitter.Create());
                info = new UI.ParticleInfo(Data.Particles.Ball);
                info.AddPhase(0, 0, 0xffb34c.ToColor(), 0.5, 0.2, 0, UI.ParticleTrajectory.Line(-90, 0.4));
                info.AddPhase(15, 60, 0xffa34c.ToColor(), 1, 0.2);
                info.AddPhase(25, 0, 0x222222.ToColor(), 0.1, 0.2);
                emitter = new UI.ParticleEmitterInfo();
                emitter.Duration = 100;
                emitter.Rate = UI.ParticleRate.Always(2);
                emitter.Generator = UI.ParticleGenerator.Same(info, UI.ParticlePoints.EngulfFromBottom(region.Rect, 1));
                Application.CurrentScene.AddEmitter(emitter.Create());
            }
        }
        public override void ResetAffect() {
            base.ResetAffect();
            LocalSummary.OnMove -= DoScorchOnMove;
            LocalSummary.OnUseAbility -= DoScorchOnAbility;
            LocalSummary.OnOffenceReady -= DoScorchOnCreature;
            LocalSummary.OnDefenceReady -= DoScorchOnCreature;
        }

        private void DoScorchOnMove(Creature creature, int @from, int to) {
            DoScorch(creature);
        }
        private void DoScorchOnAbility(AbilityCard ability, TargetArgs args) {
            DoScorch(args.Caster);
        }
        private void DoScorchOnCreature(Creature creature) {
            DoScorch(creature);
        }

        private void DoScorch(AbstractObject obj) {
            if (obj.HasFlag<Scorch>())
                obj.DamageBy(NewDamage(DamageType.Fire, Value));
        }
    }
}
